/**
 * 
 */
package fic.storefinder.client.content;

import java.sql.SQLException;


import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.ValueBoxBase.TextAlignment;
import com.google.gwt.user.client.ui.VerticalPanel;

import fic.storefinder.client.listener.UpdateDBListener;
import fic.storefinder.server.db.POITable;
import fic.storefinder.shared.serializable.POI;
import fic.storefinder.shared.utils.Constants;

/**
 * @author fic
 *
 */
public class EditPOIDialog extends DialogBox {
	
	private UpdateDBListener updateListener;
	//private POI poi;
	private TextBox nameTextBox;
	private TextBox addressTextBox;
	private TextBox cityTextBox;
	private TextBox zipTextBox;
	private TextBox lonTextBox;
	private TextBox latTextBox;
	private TextBox phoneTextBox;
	
	private Label lblName;
	private Label lblAddress;
	private Label lblCity;
	private Label lblZipCode;
	private Label lblLon;
	private Label lblLat;
	private Label lblPhone;
	
	public EditPOIDialog(final int poiEditType, final POI poi, UpdateDBListener updateListener) {
		this.updateListener = updateListener;
		//this.poi = poi;
		VerticalPanel verticalPanel = new VerticalPanel();
		setWidget(verticalPanel);
		verticalPanel.setSize("100%", "100%");
		
		FlexTable flexTable = new FlexTable();
		verticalPanel.add(flexTable);
		
		lblName = new Label("Név:");
		lblName.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		flexTable.setWidget(0, 0, lblName);
		
		nameTextBox = new TextBox();
		nameTextBox.setAlignment(TextAlignment.LEFT);
		flexTable.setWidget(0, 1, nameTextBox);
		
		lblAddress = new Label("Cím:");
		lblAddress.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		flexTable.setWidget(1, 0, lblAddress);
		
		addressTextBox = new TextBox();
		flexTable.setWidget(1, 1, addressTextBox);
		
		lblCity = new Label("Város:");
		lblCity.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		flexTable.setWidget(2, 0, lblCity);
		
		cityTextBox = new TextBox();
		flexTable.setWidget(2, 1, cityTextBox);
		
		lblZipCode = new Label("Irányítószám:");
		lblZipCode.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		flexTable.setWidget(3, 0, lblZipCode);
		
		zipTextBox = new TextBox();
		flexTable.setWidget(3, 1, zipTextBox);
		
		lblLon = new Label("Hosszúsági fok:");
		lblLon.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		flexTable.setWidget(4, 0, lblLon);
		
		lonTextBox = new TextBox();
		flexTable.setWidget(4, 1, lonTextBox);
		
		lblLat = new Label("Szélességi fok:");
		lblLat.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		flexTable.setWidget(5, 0, lblLat);
		
		latTextBox = new TextBox();
		flexTable.setWidget(5, 1, latTextBox);
		
		lblPhone = new Label("Telefonszám:");
		lblPhone.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		flexTable.setWidget(6, 0, lblPhone);
		
		phoneTextBox = new TextBox();
		flexTable.setWidget(6, 1, phoneTextBox);
		
		HorizontalPanel horizontalPanel = new HorizontalPanel();
		verticalPanel.add(horizontalPanel);
		
		if (poiEditType == Constants.EDIT_TYPE_UPDATE) {
			nameTextBox.setText(poi.getPoiTitle());
			addressTextBox.setText(poi.getAddress());
			cityTextBox.setText(poi.getCity());
			zipTextBox.setText(poi.getZipCode());
			lonTextBox.setText(poi.getLongitude() + "");
			latTextBox.setText(poi.getLatitude() + "");
			phoneTextBox.setText(poi.getPhone());
		}
		
		Button btnSave = new Button("Mentés");
		horizontalPanel.add(btnSave);
		btnSave.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				if (checkInputFields()) {
					POI poiToModify = new POI();
					poiToModify.setPoiTitle(nameTextBox.getText());
					poiToModify.setAddress(addressTextBox.getText());
					poiToModify.setCity(cityTextBox.getText());
					poiToModify.setZipCode(zipTextBox.getText());
					poiToModify.setLatitude(Float.parseFloat(latTextBox.getText()));
					poiToModify.setLongitude(Float.parseFloat(lonTextBox.getText()));
					poiToModify.setPhone(phoneTextBox.getText());
					if (poi != null) {
						poiToModify.setPoiId(poi.getPoiId());
					}
					
					if (poiEditType == Constants.EDIT_TYPE_NEW) {
						EditPOIDialog.this.save(poiToModify);
					} else if (poiEditType == Constants.EDIT_TYPE_UPDATE) {
						EditPOIDialog.this.update(poiToModify);
					}
					
					EditPOIDialog.this.hide();
				}

			}
		});
		
		Button btnCancel = new Button("Mégse");
		horizontalPanel.add(btnCancel);
		btnCancel.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				EditPOIDialog.this.hide();
			}
		});
		
		if (poiEditType == Constants.EDIT_TYPE_UPDATE) {
			Button btnDelete = new Button("Törlés");
			horizontalPanel.add(btnDelete);
			btnDelete.addClickHandler(new ClickHandler() {
				
				@Override
				public void onClick(ClickEvent event) {
					EditPOIDialog.this.delete(poi);
					EditPOIDialog.this.hide();
				}
			});
		}
	}

	
	private void save(POI poiToSave) {
		this.updateListener.insert(poiToSave);
	}
	
	private void update(POI poiToUpdate) {
		this.updateListener.update(poiToUpdate);
	}
	
	private void delete(POI poiToDelete) {
		this.updateListener.delete(poiToDelete);
	}
	
	private boolean checkInputFields() {
		boolean retValue = true;
		if ("".equals(nameTextBox.getText())) {
			lblName.setStyleName("errorTxt");
			retValue = false;
		} else {
			lblName.setStyleName("normalTxt");
		}
		
		if ("".equals(addressTextBox.getText())) {
			lblAddress.setStyleName("errorTxt");
			retValue = false;
		} else {
			lblAddress.setStyleName("normalTxt");
		}
		
		if ("".equals(cityTextBox.getText())) {
			lblCity.setStyleName("errorTxt");
			retValue = false;
		} else {
			lblCity.setStyleName("normalTxt");
		}
		
		if ("".equals(zipTextBox.getText())) {
			lblZipCode.setStyleName("errorTxt");
			retValue = false;
		} else {
			lblZipCode.setStyleName("normalTxt");
		}
		
		if ("".equals(phoneTextBox.getText())) {
			lblPhone.setStyleName("errorTxt");
			retValue = false;
		} else {
			lblPhone.setStyleName("normalTxt");
		}
		
		try {
			Float.parseFloat(latTextBox.getText());
			lblLat.setStyleName("normalTxt");
		} catch (Exception e) {
			lblLat.setStyleName("errorTxt");
			retValue = false;
		}
		try {
			Float.parseFloat(lonTextBox.getText());
			lblLon.setStyleName("normalTxt");
		} catch (Exception e) {
			lblLon.setStyleName("errorTxt");
			retValue = false;
		}
		return retValue;
	}

}
